package com.zxx.casserver.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxx.cascommon.base.exception.DataNotExistException;
import com.zxx.casserver.modules.user.entity.UserRelationDO;
import com.zxx.casserver.modules.user.entity.UserRelationVO;
import com.zxx.casserver.modules.user.mapper.UserRelationMapper;
import com.zxx.casserver.modules.user.service.IUserRelationService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户应用关联表 服务实现类
 * </p>
 *
 * @author zhoukai
 * @since 2020-08-03
 */
@Service
public class UserRelationServiceImpl extends ServiceImpl<UserRelationMapper, UserRelationDO> implements IUserRelationService {


    @Override
    public UserRelationDO saveUserRelation(UserRelationVO userRelationVO) {
        return null;
    }


    /**
     * 查询用户关系
     *
     * @param uid
     * @author zhoukai
     * @date 2020/8/5 11:41
     */
    private List<UserRelationDO> findByUser(Long uid, Integer type) {
        QueryWrapper<UserRelationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("type", type);
        return list(queryWrapper);
    }

    @Override
    public Boolean removeUserRelation(UserRelationVO userRelationVO) {
        List<UserRelationDO> userRelationDO = findByUser(userRelationVO.getUid(), userRelationVO.getType());
        if (ObjectUtils.isEmpty(userRelationDO)) {
            throw new DataNotExistException();
        }
        Set<Long> relationIdSet = new HashSet<>();
        for (UserRelationDO userRelation : userRelationDO) {
            relationIdSet.add(userRelation.getId());
        }
        return removeByIds(relationIdSet);
    }
}
